package com.theobserver.theobserver;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetDataCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wssxk on 7/11/17.
 */

public class Column {

    ColumnGroup column_group = null;

    public AVObject cur_column = null;
    public Bitmap cover = null;

    public static void getAllColumn(ColumnCallBack cb)
    {
        final ColumnCallBack tcb = cb;
        AVQuery<AVObject> column_query = new AVQuery<>("column");
        column_query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(e == null && list.size() != 0)
                {
                    ColumnGroup t_column_group = new ColumnGroup(tcb);
                    Column t_column = null;
                    for(int t = 0; t < list.size(); t ++)
                    {
                        t_column = new Column();
                        t_column_group.columns.add(t_column);
                        t_column.column_group = t_column_group;
                        t_column.cur_column = list.get(t);
                    }

                    for(int t = 0; t < list.size(); t ++)
                    {
                        t_column_group.columns.get(t).completeData(tcb);
                    }

                }
                else
                {
                    tcb.getColumn(null, 0, e);
                }
            }
        });
    }



    public static void getSubscribColumn(AVUser user, ColumnCallBack cb)
    {
        final ColumnCallBack tcb = cb;
        AVQuery<AVObject> column_query = new AVQuery<>("column_subscrib");
        column_query.include("column_id");
        column_query.whereEqualTo("user_email", user);
        column_query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(e == null && list.size() != 0)
                {
                    ColumnGroup t_column_group = new ColumnGroup(tcb);
                    Column t_column = null;
                    for(int t = 0; t < list.size(); t ++)
                    {
                        t_column = new Column();
                        t_column_group.columns.add(t_column);
                        t_column.column_group = t_column_group;
                        t_column.cur_column = list.get(t).getAVObject("column_id");
                    }

                    for(int t = 0; t < list.size(); t ++)
                    {
                        t_column_group.columns.get(t).completeData(tcb);
                    }

                }
                else
                {
                    tcb.getColumn(null, 0, e);
                }
            }
        });
    }


    //public static void get


    public void completeData(ColumnCallBack cb)
    {
        final Column thisClass = this;
        AVFile column_cover = cur_column.getAVFile("column_cover");
        column_cover.getDataInBackground(new GetDataCallback() {
            @Override
            public void done(byte[] bytes, AVException e) {
                if(e == null)
                {
                    cover = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    column_group.succed(thisClass);
                }
                else
                {
                    column_group.fail(thisClass);
                }
            }
        });
    }
}



class ColumnGroup
{
    public ArrayList<Column> columns = null;
    public int getResultCount = 0;
    public ColumnCallBack cb;

    public ColumnGroup(ColumnCallBack callback)
    {
        columns = new ArrayList<>();
        cb = callback;
    }

    public void succed(Column c)
    {
        getResultCount ++;
        if(getResultCount == columns.size())
        {
            cb.getColumn(columns, 0, null);
        }
    }

    public void fail(Column c)
    {
        columns.remove(c);
        if(columns.size() == 0)
        {
            cb.getColumn(null, News.NEWS_ERROR_NORESULT, null);
        }
        else if(getResultCount == columns.size())
        {
            cb.getColumn(columns, 0, null);
        }
    }
}
